package logic

import (
	"center/models/mysql"
	"context"
	"errors"
	"gorm.io/gorm"

	"center/centerrpc/centerrpc"
	"center/centerrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLogic {
	return &UpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateLogic) Update(in *centerrpc.UpdateRequest) (*centerrpc.UpdateResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	mysql.DB.Debug().Where("id = ?", in.UserId).Find(&user)

	if user.ID == 0 {
		return nil, errors.New("用户不存在")
	}
	user = mysql.User{
		Model:    gorm.Model{ID: user.ID},
		Account:  in.Account,
		Password: in.Password,
	}
	mysql.DB.Debug().Updates(&user)

	return &centerrpc.UpdateResponse{
		UserId: int64(user.ID),
	}, nil
}
